<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { getTypeGraphInfo, getBusinessInfo, getDisQuesInfo, getAnalysisInfo, getLocationGraphInfo, getLocationFinishGraphInfo } from "@/api/index";


const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    }
  },
  data() {
    return {
      chart: null,
      typeGraph:"",
      datas:[],
      types: []
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.getTypeGraph();
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {

      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
      	title: {
		        text: '事件类型分析',
		        subtext: '',
		    },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          },
          formatter:'{b}\n{c}%' 　
        },
         legend: {
		        data:['类型'],
           left:"right"
		    },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top:60,
          containLabel: true
        },
        yAxis: [{
          type: 'category',
          data:this.types,
          axisTick: {
            alignWithLabel: true
          },
          axisLabel:{  
            interval: 0,
          }
        }],
        xAxis: [{
          type: 'value',
          axisLabel: {
                  show: true,
                  interval: 'auto',
                  formatter: '{value} %'
                },
          axisTick: {
            show: false
          }

        }],
        series: [{
          name: '类型',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: this.datas,
          animationDuration,
        }]
      })
    },
     /** 获取不同类型的百分比报表数据 */
    getTypeGraph() {
      this.loading = true;
      getTypeGraphInfo().then(res => {
        if(res.code == 200){
          let resData = res.data;
          for(let i in resData){
              this.types.push(resData[i].typeName);
              this.datas.push(resData[i].percentage);
          }
          this.initChart();
        }
      });
    },
  }
}
</script>
